$(function () {
    BASE.PROJECTCONTENT.pageInit();
    BASE.PROJECTCONTENT.scanceProject(1);
});
var BASE = BASE || {};
BASE.PROJECTCONTENT =(function () {
    var CONTENT = null;
    /*根据页面初始化显示*/
    function pageInit() {
        if(location.href.indexOf('_user')>-1){
            getData();
            return;
        }
       initMember(1);
       BASE.COMMON.getCommentReply(1);
    }
    /*获取页面数据*/
    function getData() {
        BASE.apiGet('/device/project/detail',{id: BASE.COMMON.getId()},function (res) {
            $('#project_content').html(template('project_content_tpl',{data:res}));
            CONTENT = res.content;
            initMember(1);
            BASE.COMMON.getCommentReply(1);
        })
    }
    /*浏览*/
    function scanceProject() {
        BASE.apiPost('/device/project/click',{ id: BASE.COMMON.getId() })
    }
    /*用户信息获取判断是否关注*/
    function initMember() {
        var userId = BASE.COMMON.getUserId();
        var curId = $('#content').data('memberid');
        if (userId == curId){
            if(! $('#memberModify').hasClass('hide')){
                $('#memberModify').show();
            }
            $('#projectModify').show();
        }else {
            $('#memberNotice').show();
            BASE.COMMON.isNotice($('#memberNotice').data('memberid'),$('#memberNotice'));
        }
    }
    /*设置为接单人*/
    function setAsRecv(obj) {
        var recvMemberId =  $(obj).data('memberid');
        var sendData = {
            id:BASE.COMMON.getId(),
            recvMemberId:recvMemberId,
        };
        BASE.apiPost('/device/project/resolveRecver',sendData,function () {
            window.location.reload();
        })
    }
    /*撤单*/
    function cancelProject(obj) {
        BASE.apiPost('/device/project/cancel',{id:BASE.COMMON.getId()},function(res){
            BASE.clearpop('撤单申请成功',1000);
            setTimeout(function () {
                window.location.href='/'+BASE.COMMON.getId()+'/project_content_user.html';
            },1000);
        })
    }
    /*完成交易*/
    function doneProject(obj) {
        var recvMemberId =  $(obj).data('memberid');
        var sendData = {
            id:BASE.COMMON.getId(),
            recvMemberId:recvMemberId,
        };
        BASE.apiPost('/device/project/done',sendData,function (res) {
            BASE.clearpop('项目完成',2000);
            window.location.reload();
        })
    }
    /*所有分享送金币*/
    if(BASE.COMMON.getUserId()){
        $('body').on('click','.share-item',function () {
            BASE.postHttp('/device/project/share',{id:BASE.COMMON.getId()});
        });
        $('body').on('mouseenter','.icon-wechat',function () {
            BASE.postHttp('/device/project/share',{id:BASE.COMMON.getId()});
        });
    }
    /*修改部分start******************************/
    /*修改展示*/
    function modifyPopupShow(obj) {
        BASE.PROJECTCOMMON.getQQList();
        var title = $(obj).data('title');
        var price = $(obj).data('price');
        var priceType = $(obj).data('price_type');
        var content = $(obj).data('content');
        $('#title').val(title);
        $('#price').val(price);
        $('#price_type'+priceType).trigger('click');
        if($(obj).data('file_url')){
            $('#fileUrlLink').show().find('span').text('项目需求文档');
        }
        BASE.UPLOADANSWER.ue.txt.html(CONTENT||content);
        $('#answer_pop').show();
    }
    /*确定修改*/
    function sureModify(obj) {
        var sendData = _getAddData();
        if(!sendData){
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/project/update',sendData,function (res) {
            if(res.code != 200){
                BASE.clearpop(res.message || '修改失败');
                $(obj).attr('disabled',false);
                return;
            }
            $('#answer_pop').hide();
            window.location.href='/'+BASE.COMMON.getId()+'/project_content_user.html';
        })
    }
    /*获取修改的数据*/
    function _getAddData() {
        var sendData = {
            title: $('#title').val(),
            content: BASE.UPLOADANSWER.ue.txt.html(),
            fileUrl:$('body').data('file-url'),
            id:BASE.COMMON.getId(),
        };
        if(!sendData.title){
            BASE.clearpop('请输入项目标题');
            return false;
        }
        if(!sendData.content){
            BASE.clearpop('请输入项目简介');
            return false;
        }
        return sendData;
    }
    /*修改部分end******************************/
    return {
        initMember:initMember,
        scanceProject:scanceProject,
        cancelProject:cancelProject,
        doneProject:doneProject,
        setAsRecv:setAsRecv,
        pageInit:pageInit,

        modifyPopupShow:modifyPopupShow,
        sureModify:sureModify,
    }
})();

